<?php

namespace app\admin\model\vactivities;

use think\Db;
use think\Model;


class Ticket extends Model
{


    const TICKET_TYPE_1 = 1;//系列
    const TICKET_TYPE_2 = 2;//活动
    
    //票的类型
    //参与人选项 0 注册加入活动-报名 1 入场劵加入活动-购买 2 直接加入活动
    const IS_TICKET_0 = 0;
    const IS_TICKET_1 = 1;
    const IS_TICKET_2 = 2;
    // 表名
    protected $name = 'activity_ticket';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [

    ];



    /*
     * $aid 活动id
     * $type 活动类型  1系列  2 活动
     * $admin_id 发动票卷人
     */
    public function getTicketList($aid,$type,$admin_id =''){
        $list = $this->getModel();
        $list = $list->where('activity_id',$aid)->where('type',$type)->field('id,name,price,number,end_time,intro,temp');
        if(!empty($admin_id)){
            $list = $list->where('admin_id',$admin_id);
        }
        $list = $list->select();
        $list = collection($list)->toArray();
        if($list){
            foreach($list as $k=>$v){
                $list[$k]['price'] = pricefentoyuan($v['price']);
                $list[$k]['end_time'] = date('Y-m-d H:i:s',$v['end_time']);
            }
        }

        return $list;
    }


    /*
     * 查找有无此数据
     */
    public function findId($id){
        $rs = $this->getModel()->where('id', $id)->find();
        if(isset($rs) || !empty($rs)){
            return true;
        }else{
           return false;
        }
    }


    /*
     * 通过活动id查找入场卷id
     */
    public function getTicketByActId($aid,$type){
        if(empty($aid))
            return '';
        $list = $this->getModel()->field(['id'])->where('activity_id',$aid)->where('type',$type)->select();
        $list = collection($list)->toArray();
        $arr = [];
        if($list){
            foreach($list as $k=>$v){
                $arr[$k] = $v['id'];
            }
        }
        unset($list);
        return $arr;
    }

    /*
     * 插入全部
     */
    public  function insetAll($data){
        if(empty($data))
            return '';
        return  Db::name('activity_ticket')->insertAll($data);
    }

    /*
     *  修改所有
     */
    public  function updataAll($data){
        if(empty($data))
            return '';
        return $this->getModel()->isUpdate()->saveAll($data);
    }

    /*
     * 批量删除
     */
    public function delAll($tArr,$tnArr){
        $diffArr = array_merge(array_diff($tArr,$tnArr),array_diff($tnArr,$tArr));
        if(!empty($diffArr)){
            if(is_array($diffArr)){
                $where = 'id in('.implode(',',$diffArr).')';
            }else{
                $where = 'id='.$diffArr;
            }
            return $this->getModel()->where($where)->delete();//批量删除
        }else{
            return '';
        }

    }

}
